package com.vitabuybuy.tv.ui.mine.talent.live

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.library.common.BuildConfig
import com.vitabuybuy.tv.R
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.library.common.util.MMKVContancts
import com.library.common.util.PictureUtils
import com.luck.picture.lib.PictureSelector
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.databinding.ActivityStartLiveBinding
import com.vitabuybuy.tv.dialog.OptionsDialog
import com.vitabuybuy.tv.ui.mine.talent.live.viewmodel.StartLiveViewModel
import com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.commondef.RoomInfo
import com.vitabuybuy.tv.ui.tencent.liveroom.ui.AnchorLiveRoomActivity
import com.vitabuybuy.tv.ui.tencent.liveroom.ui.AudienceLiveRoomActivity

class StartLiveActivity : BaseActivity<StartLiveViewModel, ActivityStartLiveBinding>() {

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_start_live

    private val optionDialog by lazy { OptionsDialog(mContext) }

    private var liveCover: String? = null
    private var liveNoticeList = ArrayList<HashMap<String, Int?>>()

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.launch_live))
        mTvRight.apply {
            text = getString(R.string.confirm)
            visibility = View.VISIBLE
        }
    }

    override fun observeData() {
        viewModel.liveInfoDto.observe(this, Observer {
            val roomInfo = RoomInfo().apply {
                roomID = it.roomNumber
                mixedPlayURL = it.pullUrl
                authorName = mmkv.decodeString(MMKVContancts.EXECUTOR_NICKNAME)
                authorAvatar = mmkv.decodeString(MMKVContancts.EXECUTOR_AVATAR)
                liveId = it.id ?: -1
                userId = mmkv.decodeInt(MMKVContancts.EXECUTOR_ID).toString()
            }
            AnchorLiveRoomActivity.openActivity(mContext, roomInfo)
            finish()
        })
    }

    override fun eventHandling() {
        setOnclickNoRepeat(
            mBinding.rlLiveType, mBinding.rlStartLiveType, mBinding.ivCover,
            mBinding.rlStartTime, mBinding.rlChooseNotice, mBinding.rlWhetherOpenBeautify,
            mBinding.rlWhetherOpenChangeVoice, mBinding.rlWhetherOpenBgm
        ) {
            when (it) {
                mBinding.rlLiveType -> {
                    optionDialog.run {
                        setBtnStr(
                            getString(R.string.hunbo),
                            getString(R.string.zhuanchang)
                        )
                        onOneClick = {
                            mBinding.tvLiveType.text = getString(R.string.hunbo)
                        }
                        onTwoClick = {
                            mBinding.tvLiveType.text = getString(R.string.zhuanchang)
                        }
                        show()
                    }
                }
                mBinding.rlStartLiveType -> {
                }
                mBinding.rlStartTime -> {
                }
                mBinding.ivCover -> {
                    PictureUtils.openAluamOne(mContext, 0x11)
                }
                mBinding.rlChooseNotice -> {
                    ChooseNoticeActivity.openActivity(mContext)
                }
                mBinding.rlWhetherOpenBeautify -> {
                    optionDialog.run {
                        setBtnStr(
                            getString(R.string.kaiqi),
                            getString(R.string.guanbi)
                        )
                        onOneClick = {
                            mBinding.tvWhetherOpenBeautify.text = getString(R.string.kaiqi)
                        }
                        onTwoClick = {
                            mBinding.tvWhetherOpenBeautify.text = getString(R.string.guanbi)
                        }
                        show()
                    }
                }
                mBinding.rlWhetherOpenChangeVoice -> {
                    optionDialog.run {
                        setBtnStr(
                            getString(R.string.kaiqi),
                            getString(R.string.guanbi)
                        )
                        onOneClick = {
                            mBinding.tvWhetherOpenChangeVoice.text = getString(R.string.kaiqi)
                        }
                        onTwoClick = {
                            mBinding.tvWhetherOpenChangeVoice.text = getString(R.string.guanbi)
                        }
                        show()
                    }
                }
                mBinding.rlWhetherOpenBgm -> {
                    optionDialog.run {
                        setBtnStr(
                            getString(R.string.kaiqi),
                            getString(R.string.guanbi)
                        )
                        onOneClick = {
                            mBinding.tvWhetherOpenBgm.text = getString(R.string.kaiqi)
                        }
                        onTwoClick = {
                            mBinding.tvWhetherOpenBgm.text = getString(R.string.guanbi)
                        }
                        show()
                    }
                }
            }
        }
        mTvRight.setOnClickListener {
            when {
                mBinding.tvLiveType.text.isEmpty() -> {
                    XToast.normal(getString(R.string.pls_choose_live_type))
                }
                mBinding.etLiveTitle.text.isEmpty() -> {
                    XToast.normal(getString(R.string.pls_enter_live_title))
                }
                liveCover == null -> {
                    XToast.normal(getString(R.string.pls_upload_live_cover))
                }
//                liveNoticeList.isEmpty() -> {
//                    XToast.normal(getString(R.string.pls_choose_live_notice))
//                }
                else -> {
                    val liveParams = HashMap<String, Any?>().apply {
                        this["title"] = mBinding.etLiveTitle.text.toString()
                        this["type"] = "立即直播"
                        this["image"] = liveCover
                        this["liveType"] = mBinding.tvLiveType.text.toString()
                        this["beauty"] =
                            mBinding.tvWhetherOpenBeautify.text.toString() == getString(R.string.kaiqi)
                        this["voiceChanger"] =
                            mBinding.tvWhetherOpenChangeVoice.text.toString() == getString(R.string.kaiqi)
                        this["BackgroundMusic"] =
                            mBinding.tvWhetherOpenBgm.text.toString() == getString(R.string.kaiqi)
                    }
                    viewModel.startLive(liveParams, liveNoticeList)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            0x11 -> {
                val path = PictureSelector.obtainMultipleResult(data)[0].compressPath
                viewModel.uploadImage(path)
            }
            0x12 -> {
                liveNoticeList.clear()
                liveNoticeList.addAll(data?.extras?.getSerializable("noticeList") as ArrayList<HashMap<String, Int?>>)
                mBinding.tvCommodityChoose.text = getString(R.string.already_choose)
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                liveCover = BuildConfig.FILE_PROFILE_RELEASE.plus(msg.obj as String)
                liveCover?.run {
                    ImageAdapter.setNomalImageUrl(
                        mBinding.ivCover,
                        this, ContextCompat.getDrawable(mContext, R.drawable.default_image)
                    )
                }
            }
        }
    }


    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, StartLiveActivity::class.java)
        }
    }
}
